package com.app.scale.greendao;


import com.alibaba.fastjson.annotation.JSONField;
import com.app.scale.bean.SupplierListBean;
import com.app.scale.bean.SupplierListData;
import com.app.scale.bean.SupplierListDataDao;
import com.app.scale.bean.UserInfoBean;
import com.app.scale.bean.UserInfoBeanDao;
import com.app.scale.bean.UserInfoList;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class GreenDaoUtils {

    private static UserInfoBeanDao userInfoBeanDao = GreenDaoManager.getInstance().getmDaoSession().getUserInfoBeanDao();
    private static SupplierListDataDao supplierListDataDao = GreenDaoManager.getInstance().getmDaoSession().getSupplierListDataDao();


    public static final String TYPE_JI_JIA_YUAN = "jijiayuan" //计价员
            , TYPE_SUPPLIER = "supplier"
            ;


    public static void clearAll(){
        userInfoBeanDao.deleteAll();
        supplierListDataDao.deleteAll();
        EventBus.getDefault().post(new GreenDaoUtils.EventDateChange(EventDateChange.TYPE_ALL));
    }


    /**
     * 增加
     **/
    public static void userBeanInsertData(UserInfoList infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        userInfoBeanDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void supplierBeanInsertData(SupplierListBean infoBean, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        supplierListDataDao.insertOrReplaceInTx(infoBean.getList());
        callBack.onDataInsert(infoBean.getList().size());
    }

    public static void supplierBeanReplace(SupplierListData data, GreenDaoInserCallBack callBack) {
        if (data == null ) return;
        supplierListDataDao.insertOrReplaceInTx(data);
        callBack.onDataInsert(1);
    }


    public static String  checkIsUserOrSupplier(String no){
        List<UserInfoBean> userList = userInfoBeanDao.queryBuilder().where(UserInfoBeanDao.Properties.Cdno.eq(no)).build().list();
       if(userList.size()>0){
           return "user";
       }
        List<SupplierListData> supplierList = supplierListDataDao.queryBuilder().where(SupplierListDataDao.Properties.Cdno.eq(no)).build().list();
        if(supplierList.size()>0){
            return "supplier";
        }

        if(no.contains("http")){
           String newNo = no.replace("http//www.fdbcdsj.cn8880/QRHome/Scan/","").trim();
            supplierList = supplierListDataDao.queryBuilder().where(SupplierListDataDao.Properties.Bno.eq(newNo)).build().list();
            if(supplierList.size()>0){
                return "supplierBno";
            }
        }
       return "";
    }




    public static class EventDateChange {
        public static final String TYPE_ALL = "99";
        public String type;

        public EventDateChange(String type) {
            this.type = type;
        }
    }

    public interface GreenDaoInserCallBack {
        void onDataInsert(int count);
    }

    @JSONField(serialize = false)
    public static String getTypeName(String map_type) {
        switch (map_type) {
            case GreenDaoUtils.TYPE_JI_JIA_YUAN:
                return "计价员";
            case GreenDaoUtils.TYPE_SUPPLIER:
                return "供应商";
            default:
                return "未知";
        }
    }
}
